//
//  CompetitionViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/7/19.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "CompetitionViewController.h"
#import "ContainerViewController.h"
#import "YYInfiniteLoopView.h"
#import "LearnSkillMiddleCell.h"
#import "ListenStoryHeaderCell.h"
#import "OrganationSeekCell.h"
#import "NewPagedFlowView.h"
#import "PGIndexBannerSubiew.h"
#import "OrganizationViewController.h"
#import "MatchListModel.h"
#import "CompetionBottomView.h"
#import "InstitutionsDetailContainViewController.h"
#import "CompetionHomeBottomRewardCell.h"
#import "CompetionBackDetailController.h"
#import "SignUpViewController.h"
#import "MyInstitutionViewController.h"
@interface CompetitionViewController ()<YYInfiniteLoopViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,NewPagedFlowViewDataSource,NewPagedFlowViewDelegate,UIViewControllerTransitioningDelegate>

@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property(nonatomic,strong)NSMutableArray *imageArra;
@property(nonatomic,strong)NewPagedFlowView *pageFlowView;
@property(nonatomic,strong)UIScrollView *bottomScrollView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *peopleCountLabel;
@property(nonatomic,strong)UILabel *timeLabel;
@property(nonatomic,strong)UIView *bottomView;
@property(nonatomic,strong)UILabel *navLabel;
@property(nonatomic,strong)NSMutableArray *competionBackDataArray;
@end

@implementation CompetitionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.subviews.firstObject.alpha = 0;
    self.navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    self.navLabel.backgroundColor = [UIColor clearColor];
    self.navLabel.textColor = [UIColor whiteColor];
    self.navLabel.font = [UIFont systemFontOfSize:17.0];
    self.navLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = self.navLabel;
//    self.title = @"我赛";
//    self.navigationController.title = @"";
    
    [self getMiddleLatestMatchInformationWithShowcurrentpage:@"10"];
    [self createCollectionUI];
    [self getCompetionBackDataWithShowPage:@"10"];
    // Do any additional setup after loading the view.
}
-(void)createNavagationBackBtn{
    [super createNavagationBackBtn];
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0 , 0, 0);
    UIImageView *backImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    backImg.frame = CGRectMake(-10, 10,0, 0);
    [backBtn addSubview:backImg];
    [backImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backBtn.mas_left).offset(-10);
        make.centerY.equalTo(backBtn.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    [self.navigationItem setLeftBarButtonItem:leftButton];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    CGFloat offset = self.collectionView.contentOffset.y;
    [self.navigationController.navigationBar
     setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:201/255.0 blue:31/255.0 alpha:(offset / 64)>0.99?0.99:(offset / 64)]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar
     setShadowImage:[UIImage new]];
//    CGFloat minAlphaOffset = 0;
//    CGFloat maxAlphaOffset = scaleTopAndBottom(175);
//    CGFloat offset = self.collectionView.contentOffset.y;
//    CGFloat alpha = (offset - minAlphaOffset) / (maxAlphaOffset - minAlphaOffset);
//    self.navigationController.navigationBar.subviews.firstObject.alpha = alpha;
}
-(UIImage*) imageWithColor:(UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
    
}
#pragma mark -图片轮播
- (void)loopViewDelegateAndAddToFatherView:(UIView *)faterView{
    NSArray *array = @[@"banner(1).jpg",@"元旦狗.jpg"];
    // 类方法
    YYInfiniteLoopView *loopView = [YYInfiniteLoopView
                                    infiniteLoopViewWithImageUrls:array
                                    titles:nil
                                    didSelectedImage:^(NSInteger index) {
                                        NSLog(@"%zd",index);
                                    }];
    // 设置代理
    loopView.delegate = self;
    loopView.timeInterval = 5;
    loopView.currentPageIndicatorColor = [UIColor yellowColor];
    loopView.pagePosition = InfiniteLoopViewPagePositionCenter;
    loopView.frame = CGRectMake(0, 0, WIDTH, scaleTopAndBottom(190));
    [faterView addSubview:loopView];
}
#pragma mark - YYInfiniteLoopViewDelegate
- (void)infiniteLoopView:(YYInfiniteLoopView *)infiniteLoopView
        didSelectedImage:(NSInteger)selectedImageIndex {
    [self didSelectedImageWithIndex:selectedImageIndex];
}
- (void)didSelectedImageWithIndex:(NSInteger)index {
//    MyInstitutionViewController *institVc = [[MyInstitutionViewController alloc] init];
//    [self.navigationController pushViewController:institVc animated:YES];
    NSLog(@"点击了%ld",index);
}
-(void)viewDidDisappear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}
#pragma mark -创建collectionView
-(void)createCollectionUI{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    [self.view addSubview:self.collectionView];
    if (@available(iOS 11.0, *)) {
        self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-49);
    }];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeaderone"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeadertwo"];
        [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeaderthree"];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"collectionFooter"];
     UINib *nib1 = [UINib nibWithNibName:@"ListenStoryHeaderCell" bundle:nil];
     [self.collectionView registerNib:nib1 forCellWithReuseIdentifier:@"ListenStoryHeaderCell"];
    UINib *nib3 = [UINib nibWithNibName:@"LearnSkillMiddleCell" bundle:nil];
    [self.collectionView registerNib:nib3 forCellWithReuseIdentifier:@"LearnSkillMiddleCell"];
    [self.collectionView registerClass:[OrganationSeekCell class] forCellWithReuseIdentifier:@"OrganationSeekCell"];
    [self.collectionView registerClass:[CompetionHomeBottomRewardCell class] forCellWithReuseIdentifier:@"CompetionHomeBottomRewardCell"];
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 4;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 3) {
        return self.competionBackDataArray.count;
    }
    return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ListenStoryHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListenStoryHeaderCell" forIndexPath:indexPath];
        [self loopViewDelegateAndAddToFatherView:cell.contentView];
        return cell;
    }else if (indexPath.section == 1){
        LearnSkillMiddleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LearnSkillMiddleCell" forIndexPath:indexPath];
       [self setupUI:cell.contentView];
        return cell;
    }else if(indexPath.section == 2){
        CompetionBottomView *view = [[CompetionBottomView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, scaleTopAndBottom(230))];
        view.goBlock = ^(NSString *insitiId) {
            InstitutionsDetailContainViewController *vc = [[InstitutionsDetailContainViewController alloc] init];
            vc.instituId = insitiId;
            [self.navigationController pushViewController:vc animated:YES];
        };
        OrganationSeekCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OrganationSeekCell" forIndexPath:indexPath];
        for (UIView *subvies in cell.contentView.subviews) {
            [subvies removeFromSuperview];
        }
        [cell.contentView addSubview:view];
        UIView *moreBottomVeiw = [[UIView alloc] init];
        [cell.contentView addSubview:moreBottomVeiw];
        [moreBottomVeiw mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(cell.contentView.mas_bottom).offset(scaleTopAndBottom(-15));
            make.size.mas_equalTo(CGSizeMake(cell.contentView.bounds.size.width, scaleTopAndBottom(40)));
            make.left.equalTo(cell.contentView);
        }];
//        [cell.contentView addSubview:self.bottomView];
//        self.bottomView.backgroundColor  =  [UIColor redColor];
        [self createBottomMoreBtn:moreBottomVeiw];
        return cell;
    }else{
        CompetionHomeBottomRewardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CompetionHomeBottomRewardCell" forIndexPath:indexPath];
        for (UIView *subView in cell.contentView.subviews) {
            [subView removeFromSuperview];
        }
        cell.model = self.competionBackDataArray[indexPath.row];
        return cell;
    }

}
//-(UIView *)bottomView{
//    if (_bottomView == nil) {
//        _bottomView =  [[UIView alloc] initWithFrame:CGRectMake(0, scaleTopAndBottom(250), WIDTH, scaleTopAndBottom(40))];
//    }
//    return _bottomView;
//}
#pragma mark - 注册collectionView的头部视图
//collectionview的头部视图的设置
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        if (indexPath.section == 0) {
            return nil;
        }else if (indexPath.section == 1) {
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeaderone" forIndexPath:indexPath];
            for (UIView *subView in view.subviews) {
                [subView removeFromSuperview];
            }
            [self addHeaderViewContent:view AndChineseTitle:@"最新赛事" AndEnglishTitle:@"Latest events"];
            return view;
            
        }else if(indexPath.section == 2){
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeadertwo" forIndexPath:indexPath];
            for (UIView *subView in view.subviews) {
                [subView removeFromSuperview];
            }
            [self addSpecialHeaderViewContent:view AndChineseTitle:@"全国优秀机构" AndEnglishTitle:@"National excellent organization"];
            return view;
        }else{
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeaderthree" forIndexPath:indexPath];
            for (UIView *subView in view.subviews) {
                [subView removeFromSuperview];
            }
            [self addHeaderViewContent:view AndChineseTitle:@"赛事回顾" AndEnglishTitle:@"Event review"];
            return view;
        }
  
    }else{
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"collectionFooter" forIndexPath:indexPath];
        view.backgroundColor = COLOR_HEX(0xE8E8E8);
        return view;
    }
    
}
//设置头部视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(WIDTH, scaleTopAndBottom(70));
}
//设置尾视图
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section == 3) {
        return CGSizeZero;
    }
    return CGSizeMake(WIDTH, scaleTopAndBottom(5));
}

//设置item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
        if (indexPath.section == 0) {
            return CGSizeMake(WIDTH, scaleTopAndBottom(190));
        }else if (indexPath.section == 1){
            return CGSizeMake(WIDTH, scaleTopAndBottom(230) + scaleTopAndBottom(20) + scaleTopAndBottom(10));
        }else if(indexPath.section == 2) {
            return CGSizeMake(WIDTH, scaleTopAndBottom(240) + scaleTopAndBottom(50));
        }else{
            return CGSizeMake(WIDTH, scaleTopAndBottom(190) + scaleTopAndBottom(20));
        }
}
//设置collectionview中的item的行间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
        if (section == 0) {
            return 0;
        }
        return scaleTopAndBottom(1);
}
//设置collectionview中的item的列间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return scaleLeftAndRight(1);
}
#pragma mark -选中item的状态
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 3) {
        CompetionBackDetailController *detailvc = [[CompetionBackDetailController alloc] init];
        MatchListModel *model = self.competionBackDataArray[indexPath.row];
        detailvc.matchId = model.Id;
        [self.navigationController pushViewController:detailvc animated:YES];
    }
}
#pragma mark -头部视图
-(void)addHeaderViewContent:(UIView *)titleView AndChineseTitle:(NSString *)chineTile AndEnglishTitle:(NSString *)engTitle{
    CGSize size = [chineTile boundingRectWithSize:CGSizeMake(WIDTH, 300) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.f]} context:nil].size;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
//    label.backgroundColor = [UIColor redColor];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:chineTile];
    label.attributedText = attr;
    label.attributedText = attr;
    label.textAlignment = NSTextAlignmentCenter;
//    label.font = [UIFont systemFontOfSize:17];
    label.textColor = COLOR_HEX(0x121212);
    [titleView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(titleView.mas_centerX);
//        make.centerY.equalTo(titleView.mas_centerY).offset(scaleTopAndBottom(-5));
        make.top.equalTo(titleView.mas_top).offset(scaleTopAndBottom(15));
        make.width.mas_equalTo(size.width + scaleLeftAndRight(10));
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    UIView *llineView = [[UIView alloc] init];
    llineView.backgroundColor = COLOR_HEX(0x4C4C4C);
    [titleView addSubview:llineView];
    [llineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(label.mas_left).offset(-4);
        make.size.mas_equalTo(CGSizeMake(5, 1));
        make.top.equalTo(label.mas_centerY);
    }];
    UIView *rlineView = [[UIView alloc] init];
    rlineView.backgroundColor = COLOR_HEX(0x4C4C4C);
    [titleView addSubview:rlineView];
    [rlineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label.mas_right).offset(4);
        make.size.mas_equalTo(CGSizeMake(5, 1));
        make.top.equalTo(label.mas_centerY);
    }];
    
    UILabel *bottomLabel = [[UILabel alloc] init];
    bottomLabel.text = engTitle;
    bottomLabel.font = [UIFont systemFontOfSize:12];
    bottomLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
    bottomLabel.textColor = COLOR_HEX(0x333333);
    bottomLabel.adjustsFontSizeToFitWidth = YES;
    bottomLabel.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:bottomLabel];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom).offset(0);
        make.width.mas_equalTo(WIDTH);
        make.height.mas_equalTo(scaleTopAndBottom(20));
        make.left.equalTo(titleView.mas_left);
    }];
    
}
#pragma mark -头部视图
-(void)addSpecialHeaderViewContent:(UIView *)titleView AndChineseTitle:(NSString *)chineTile AndEnglishTitle:(NSString *)engTitle{
        CGSize size = [chineTile boundingRectWithSize:CGSizeMake(WIDTH, 300) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.f]} context:nil].size;
//    CGSize size = [chineTile sizeWithFont:[UIFont boldSystemFontOfSize:17] constrainedToSize:CGSizeMake(self.view.frame.size.width, 300) lineBreakMode:NSLineBreakByWordWrapping];
    UILabel *label = [[UILabel alloc] init];
//    label.backgroundColor = [UIColor redColor];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:chineTile];
    label.attributedText = attr;
    label.textAlignment = NSTextAlignmentCenter;
//    label.font = [UIFont systemFontOfSize:17];
    label.textColor = COLOR_HEX(0x121212);
    [titleView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(titleView.mas_centerX);
        //        make.centerY.equalTo(titleView.mas_centerY).offset(scaleTopAndBottom(-5));
        make.top.equalTo(titleView.mas_top).offset(scaleTopAndBottom(15));
        make.width.mas_equalTo(size.width + scaleLeftAndRight(10));
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    UIView *llineView = [[UIView alloc] init];
    llineView.backgroundColor = COLOR_HEX(0x4C4C4C);
    [titleView addSubview:llineView];
    [llineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(label.mas_left).offset(0);
        make.size.mas_equalTo(CGSizeMake(5, 1));
        make.top.equalTo(label.mas_centerY);
    }];
    UIView *rlineView = [[UIView alloc] init];
    rlineView.backgroundColor = COLOR_HEX(0x4C4C4C);
    [titleView addSubview:rlineView];
    [rlineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label.mas_right).offset(0);
        make.size.mas_equalTo(CGSizeMake(5, 1));
        make.top.equalTo(label.mas_centerY);
    }];
    
    UILabel *bottomLabel = [[UILabel alloc] init];
    bottomLabel.text = engTitle;
    bottomLabel.font = [UIFont systemFontOfSize:12];
    bottomLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
    bottomLabel.textColor = COLOR_HEX(0x333333);
    bottomLabel.adjustsFontSizeToFitWidth = YES;
    bottomLabel.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:bottomLabel];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom).offset(0);
        make.width.mas_equalTo(WIDTH);
        make.height.mas_equalTo(scaleTopAndBottom(20));
        make.left.equalTo(titleView.mas_left);
    }];
    
}

#pragma mark -创建学本领中间的图片立体手动轮播
- (void)setupUI:(UIView *)view{
    //设置高度 WIDTH * 9 / 16
    for (UIView *views in view.subviews) {
        [views removeFromSuperview];
    }
    _pageFlowView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0, scaleTopAndBottom(-20), WIDTH, scaleTopAndBottom(230) + scaleTopAndBottom(15))];
    _pageFlowView.delegate = self;
    _pageFlowView.dataSource = self;
    _pageFlowView.minimumPageAlpha = 0.1;
    _pageFlowView.isCarousel = NO;
    _pageFlowView.orientation = NewPagedFlowViewOrientationHorizontal;
    _pageFlowView.isOpenAutoScroll = YES;
    
    //初始化pageControl
    /****************************
     使用导航控制器(UINavigationController)
     如果控制器中不存在UIScrollView或者继承自UIScrollView的UI控件
     请使用UIScrollView作为NewPagedFlowView的容器View,才会显示正常,如下
     *****************************/
    _bottomScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, scaleTopAndBottom(230) + scaleTopAndBottom(15))];
//    _bottomScrollView.backgroundColor = [UIColor redColor];
    [_bottomScrollView addSubview:_pageFlowView];
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, scaleTopAndBottom(190) + scaleTopAndBottom(15), WIDTH, scaleTopAndBottom(40))];
    
    [self createMiddleMoreBtn:bottomView];
    [_bottomScrollView addSubview:bottomView];
    [_pageFlowView reloadData];
    [view addSubview:_bottomScrollView];

}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark NewPagedFlowView Delegate
- (CGSize)sizeForPageInFlowView:(NewPagedFlowView *)flowView {
//    return CGSizeMake(WIDTH -scaleLeftAndRight(60), scaleTopAndBottom(200) + scaleTopAndBottom(15));
        return CGSizeMake(WIDTH -scaleLeftAndRight(60),scaleTopAndBottom(200) + scaleTopAndBottom(10));
}
- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    MatchListModel *model = self.imageArra[subIndex];
    SignUpViewController *signupVc = [[SignUpViewController alloc] init];
    signupVc.matchID = model.Id;
    signupVc.matchStarttime = model.submitEndTime;
    [self.navigationController pushViewController:signupVc animated:YES];
    NSLog(@"点击了第%ld",(long)subIndex);
}

#pragma mark NewPagedFlowView Datasource
- (NSInteger)numberOfPagesInFlowView:(NewPagedFlowView *)flowView {
    return self.imageArra.count;
}
- (UIView *)flowView:(NewPagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    PGIndexBannerSubiew *bannerView = (PGIndexBannerSubiew *)[flowView dequeueReusableCell];
    bannerView.backgroundColor = [UIColor redColor];
    bannerView = [[PGIndexBannerSubiew alloc] initWithFrame:CGRectMake(0, 0,WIDTH - scaleLeftAndRight(60), scaleTopAndBottom(200) + scaleTopAndBottom(20))];
//    bannerView.backgroundColor = [UIColor redColor];
    [self createSubviewOfView:bannerView.bottomView];
    bannerView.tag = index;
    bannerView.layer.cornerRadius = scaleTopAndBottom(4);
    bannerView.layer.masksToBounds = YES;

    MatchListModel *model = self.imageArra[index];
    [bannerView.mainImageView sd_setImageWithURL:[NSURL URLWithString:model.ImgPath] placeholderImage:[UIImage imageNamed:@""]];
    _titleLabel.text = model.name;
    _titleLabel.font = [UIFont systemFontOfSize:16];
    _peopleCountLabel.text = [NSString stringWithFormat:@"报名人数:%@",model.count];
    
    _timeLabel.text = [self getHour:@"" systemTime:model.submitEndTime];
    _timeLabel.font = [UIFont systemFontOfSize:13];
    _timeLabel.textColor = COLOR_HEX(0x666666);
    _peopleCountLabel.font = [UIFont systemFontOfSize:13];
    _peopleCountLabel.textColor = COLOR_HEX(0x666666);
    NSLog(@"字号大小%@",_timeLabel.font);
    //在这里下载网络图片
    return bannerView;
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(NewPagedFlowView *)flowView {
    NSLog(@"ViewController 滚动到了第%ld页",pageNumber);
}
-(void)createSubviewOfView:(UIView *)view{
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [view addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_top).offset(scaleTopAndBottom(15));
        make.left.and.right.equalTo(view);
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    _timeLabel = [[UILabel alloc] init];
    _timeLabel.textAlignment = NSTextAlignmentRight;
    [view addSubview:_timeLabel];
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_bottom).offset(scaleTopAndBottom(3));
        make.left.equalTo(view.mas_left).offset(scaleLeftAndRight(0));
        make.height.mas_equalTo(scaleTopAndBottom(20));
        make.width.mas_equalTo(WIDTH/2);
    }];
    _peopleCountLabel = [[UILabel alloc] init];
    [view addSubview:_peopleCountLabel];
    [_peopleCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(_titleLabel.mas_bottom).offset(scaleTopAndBottom(3));
        make.left.equalTo(_timeLabel.mas_right).offset(scaleLeftAndRight(10));
        make.height.mas_equalTo(scaleTopAndBottom(20));
        make.right.equalTo(view.mas_right);
    }];
}
-(void)createMiddleMoreBtn:(UIView *)view{
    
    UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreBtn setTitle:@"更多" forState:UIControlStateNormal];
    moreBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [moreBtn setTitleColor:COLOR_HEX(0xA2A2A2) forState:UIControlStateNormal];
    [moreBtn addTarget:self action:@selector(gotoMatchListView:) forControlEvents:UIControlEventTouchUpInside];
    moreBtn.layer.borderColor = COLOR_HEX(0xA2A2A2).CGColor;
    moreBtn.layer.cornerRadius = scaleTopAndBottom(10);
    moreBtn.layer.masksToBounds = YES;
    moreBtn.layer.borderWidth = 1.0f;
    [view addSubview:moreBtn];
    [moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(58), scaleTopAndBottom(20)));
        make.top.equalTo(view.mas_top).offset(scaleTopAndBottom(15));
        make.centerX.equalTo(view.mas_centerX);
    }];
    
}
-(void)createBottomMoreBtn:(UIView *)view{
    UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreBtn setTitle:@"更多" forState:UIControlStateNormal];
    moreBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [moreBtn setTitleColor:COLOR_HEX(0xA2A2A2) forState:UIControlStateNormal];
    [moreBtn addTarget:self action:@selector(gotoInstituteListView:) forControlEvents:UIControlEventTouchUpInside];
    moreBtn.layer.borderColor = COLOR_HEX(0xA2A2A2).CGColor;
    moreBtn.layer.cornerRadius = scaleTopAndBottom(10);
    moreBtn.layer.masksToBounds = YES;
    moreBtn.layer.borderWidth = 1.0f;
    [view addSubview:moreBtn];
    [moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(60), scaleTopAndBottom(20)));
        make.top.equalTo(view.mas_top).offset(scaleTopAndBottom(15));
        make.centerX.equalTo(view.mas_centerX);
    }];
}
-(void)gotoMatchListView:(UIButton *)btn{
    ContainerViewController *containvc = [[ContainerViewController alloc] init];
    [self.navigationController pushViewController:containvc animated:YES];
}
-(void)gotoInstituteListView:(UIButton *)btn{
    OrganizationViewController *orgaVc = [[OrganizationViewController alloc] init];
    [self.navigationController pushViewController:orgaVc animated:YES];
}
#pragma mark -数组懒加载

-(NSMutableArray *)imageArra{
    if (_imageArra == nil) {
        _imageArra = [[NSMutableArray alloc] init];
    }
    return _imageArra;
}

/**
 获取最新赛事
 */
-(void)getMiddleLatestMatchInformationWithShowcurrentpage:(NSString *)currentpage{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *url = ServerIp@"/matchApp/newMatchList";
    NSDictionary *param = @{@"currentPage":@"1",@"showCount":currentpage,@"type":@"true"};
    [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *dataArra = jsonDic[@"matchList"];
        for (NSDictionary *dict in dataArra) {
            MatchListModel *model = [MatchListModel modelWithDictionary:dict];
            [self.imageArra addObject:model];
        }
        [self.collectionView reloadData];
        NSLog(@"%@",dataArra);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败%@",error);
    }];
    
}

/**
 根据时间戳获取到时间差
 */
- (NSString* )getHour:(NSString *)takeCarTime systemTime:(NSString *)systemTime

{
    
    //  时区相差8个小时 加上这个时区即是北京时间
    
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    
    NSInteger delta = [timeZone secondsFromGMT];
    
    NSDate *DRstartDate = [NSDate date];
    NSDate *DRendDate = [[NSDate alloc] initWithTimeIntervalSince1970:[systemTime doubleValue]/1000 + delta];
    NSLog(@"开始时间%@",DRstartDate);
    NSLog(@"结束时间%@",DRendDate);
    // 日历对象 （方便比较两个日期之间的差距）
    
    NSTimeInterval interTime = [DRendDate timeIntervalSinceNow];
    //计算天数、时、分、秒
    
    int days = ((int)interTime)/(3600*24);
    
    int hours = ((int)interTime)%(3600*24)/3600;
    
    int minutes = ((int)interTime)%(3600*24)%3600/60;
    
    NSString *dateContent = [[NSString alloc] initWithFormat:@"截止倒计时:%i天%i时%i分",days,hours,minutes];
    NSLog(@"嘎嘎嘎嘎%@",dateContent);
    return dateContent;
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offset = self.collectionView.contentOffset.y;
    [self.navigationController.navigationBar
     setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:201/255.0 blue:31/255.0 alpha:(offset / 64)>0.99?0.99:(offset / 64)]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar
     setShadowImage:[UIImage new]];
    if (scrollView.contentOffset.y > scaleTopAndBottom(175)) {
        self.navLabel.text = @"我赛";
    }else{
        self.navLabel.text = @"";
    }
}
-(void)viewWillDisappear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}
-(NSMutableArray *)competionBackDataArray{
    if (_competionBackDataArray == nil) {
        _competionBackDataArray = [[NSMutableArray alloc] init];
    }
    return _competionBackDataArray;
}
/**
 获取赛事回顾数据
 */
-(void)getCompetionBackDataWithShowPage:(NSString *)showpage{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *url = ServerIp@"/matchApp/newMatchList";
    NSDictionary *param = @{@"currentPage":@"1",@"showCount":showpage,@"type":@"false"};
    [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *dataArra = jsonDic[@"matchList"];
        for (NSDictionary *dict in dataArra) {
            MatchListModel *model = [MatchListModel modelWithDictionary:dict];
            [self.competionBackDataArray addObject:model];
        }
//        NSIndexSet *indexset = [[NSIndexSet alloc] initWithIndex:3];
//        [self.collectionView reloadSections:indexset];
        NSLog(@"%@",dataArra);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败%@",error);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
